home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / bipl.zip / PROGS.ZIP / MORSE.ICN < prev    next >
Text File  |  1992-09-28  |  3KB  |  96 lines

  1. ############################################################################
  2. #
  3. #    File:     morse.icn
  4. #
  5. #    Subject:  Program to convert string to Morse code
  6. #
  7. #    Author:   Ralph E. Griswold and Robert J. Alexander
  8. #
  9. #    Date:     May 5, 1992
  10. #
  11. ###########################################################################
  12. #
  13. #  If "morse" is invoked without arguments, a Morse code table is
  14. #  printed.  If words are entered as arguments, the Morse code
  15. #  conversion is printed in dots and dashes.  If the first character of
  16. #  the first argument is a dot or dash, the arguments are takes as Morse
  17. #  code and converted to a string.
  18. #
  19. ############################################################################
  20. #
  21. #  Links: colmize
  22. #
  23. ############################################################################
  24.  
  25. link colmize
  26.  
  27. procedure main(arg)
  28.    local lst, c, s
  29.    if *arg = 0 then {
  30.       lst := []
  31.       every c := !(&ucase || "    " || &digits) do {
  32.      put(lst,c || " " || morse(c))
  33.      }
  34.       every write(colmize(lst))
  35.       }
  36.    else {
  37.       s := ""
  38.       every s ||:= !arg || " "
  39.       s := trim(s)
  40.       write((if any('.-',s) then unmorse else morse)(s))
  41.       }
  42. end
  43.  
  44.  
  45. ############################################################################
  46. #
  47. #     This procedure converts the string s to its Morse code equivalent.
  48. #
  49. ############################################################################
  50.  
  51. procedure morse(s)
  52.    local i, t, c, x
  53.    static morsemeander, morseindex
  54.  
  55.    initial {
  56.       morsemeander :=
  57.       "....------.----..---.-.---...--.--.-..--..-.--....-.-.-...-..-....."
  58.       morseindex :=
  59.       "TMOT09TTT1T8TT2GQTTTJTZ7T3NKYTTCTTTTDXTTWPTB64EARTTLTVTIUFTSH5"
  60.       }
  61.  
  62.    x := ""
  63.    every c := !map(s,&lcase,&ucase) do
  64.       if not(i := find(c,morseindex)) then x ||:= "    "
  65.       else {
  66.      t := morsemeander[i+:6]
  67.      x ||:= t[find("-",t)+1:0] || " "
  68.      }
  69.    return x
  70. end
  71.  
  72.  
  73. ############################################################################
  74. #
  75. #     This procedure converts Morse code string s to its character string
  76. #     equivalent.
  77. #
  78. ############################################################################
  79.  
  80. procedure unmorse(s)
  81.    local x, t, c
  82.    x := ""
  83.    s ? {
  84.       until pos(0) do {
  85.      tab(many(' \t'))
  86.      t := tab(upto(' \t') | 0)
  87.      if t == "" then next
  88.      x ||:= (every c := !(&ucase || &digits) do {
  89.         if trim(morse(c)) == t then break c
  90.         }) | "?"
  91.      }
  92.       }
  93.    return x
  94. end
  95.  
  96.